﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MediaPortal.TvServer.WebServices;
using SetupTv;
using TvLibrary.Log;

namespace TvServerPlugin
{
    public partial class MPWebServicesSetup : SectionSettings
    {
        public MPWebServicesSetup()
        {
            InitializeComponent();
        }

        public override void OnSectionDeActivated()
        {
            Log.WriteInfo("Configuration closed");
            SaveConfig();
            base.OnSectionDeActivated();
        }
        public override void OnSectionActivated()
        {
            Log.WriteInfo("Configuration opened");
            LoadConfig();
            base.OnSectionActivated();
        }

        private TransportMethod GetTransportMethod(string s)
        {
            switch (s)
            {
                case "Filename":
                    return TransportMethod.Filename;
                case "NamedPipes":
                    return TransportMethod.NamedPipe;
                case "StandardIn":
                    return TransportMethod.StandardIn;
                case "StandardOut":
                    return TransportMethod.StandardOut;
            }
            return TransportMethod.NamedPipe;
        }
        public void LoadConfig()
        {
            Settings.LoadSettings();

            nudPort.Value = Settings.HttpPort;
            cbPlayerType.SelectedIndex = Settings.PlayerType;
            edPlayer.Text = Settings.ClientPlayer;
            edUid.Text = Settings.WebUsername;
            edPwd.Text = Settings.WebPassword;
            edURL.Text = Settings.StreamURL;
            edScraper.Text = Settings.ScraperURL;

            nudThumbHeight.Value = Settings.ThumbHeight;
            nudThumbWidth.Value = Settings.ThumbWidth;

            thTv.Text = Settings.Thumbs.TV;
            thRadio.Text = Settings.Thumbs.Radio;
            thPictures.Text = Settings.Thumbs.Pictures;

            edMovies.Text = Settings.DBLocations.Movies;
            edMusic.Text = Settings.DBLocations.Music;
            edPictures.Text = Settings.DBLocations.Pictures;
            edTvSeries.Text = Settings.DBLocations.TVSeries;
            edMovingPictures.Text = Settings.DBLocations.MovingPictures;

            grid.Rows.Clear();
            foreach (EncoderConfig cfg in Settings.EncoderConfigs)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(grid);
                row.Cells[0].Value = cfg.DisplayName;
                row.Cells[1].Value = cfg.UseTranscoding;
                row.Cells[2].Value = cfg.FileName;
                row.Cells[3].Value = cfg.Args;
                row.Cells[4].Value = cfg.InputMethod.ToString();
                row.Cells[5].Value = cfg.OutputMethod.ToString();
                grid.Rows.Add(row);
            }
            grid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        public void SaveConfig()
        {
            Settings.HttpPort = (int)nudPort.Value;
            Settings.PlayerType = cbPlayerType.SelectedIndex;
            Settings.ClientPlayer = edPlayer.Text;
            Settings.WebUsername = edUid.Text;
            Settings.WebPassword = edPwd.Text;
            Settings.StreamURL = edURL.Text;
            Settings.ScraperURL = edScraper.Text;

            Settings.ThumbHeight = (int)nudThumbHeight.Value;
            Settings.ThumbWidth = (int)nudThumbWidth.Value;
            Settings.Thumbs.TV = thTv.Text;
            Settings.Thumbs.Radio = thRadio.Text;
            Settings.Thumbs.Pictures = thPictures.Text;

            Settings.DBLocations.Movies = edMovies.Text;
            Settings.DBLocations.Music = edMusic.Text;
            Settings.DBLocations.Pictures = edPictures.Text; ;
            Settings.DBLocations.TVSeries = edTvSeries.Text;
            Settings.DBLocations.MovingPictures = edMovingPictures.Text;

            Settings.EncoderConfigs.Clear();
            foreach (DataGridViewRow row in grid.Rows)
            {
                if (row.Cells[0].Value != null)
                    Settings.EncoderConfigs.Add(new EncoderConfig(
                        row.Cells[0].Value.ToString(), 
                        (bool)row.Cells[1].Value, 
                        row.Cells[2].Value.ToString(), 
                        row.Cells[3].Value.ToString(), 
                        GetTransportMethod(row.Cells[4].Value.ToString()), 
                        GetTransportMethod(row.Cells[5].Value.ToString())));
            }
            Settings.SaveSettings();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openDlg.ShowDialog() == DialogResult.OK)
                edMovies.Text = openDlg.FileName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openDlg.ShowDialog() == DialogResult.OK)
                edMusic.Text = openDlg.FileName;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (openDlg.ShowDialog() == DialogResult.OK)
                edPictures.Text = openDlg.FileName;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (openDlg.ShowDialog() == DialogResult.OK)
                edTvSeries.Text = openDlg.FileName;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (openDlg.ShowDialog() == DialogResult.OK)
                edMovingPictures.Text = openDlg.FileName;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                thTv.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                thRadio.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                thPictures.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string filter = openDlg.Filter;
            openDlg.Filter = "Applications (*.exe)|*.exe";
            if (openDlg.ShowDialog() == DialogResult.OK)
                edPlayer.Text = openDlg.FileName;
            openDlg.Filter = filter;
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = Settings.BaseDir + "\\MPWebServices\\MPWebServices_help.html";
            proc.StartInfo.UseShellExecute = true;
            proc.Start();
        }
    }

}
